package tx.os3.clientserver.client.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import tx.os3.clientserver.client.model.FileRepresentation.FileType;

/**
 * Representation of the files on the server.
 * 
 * @author Thomas Middeldorp
 */
public class FileListModel extends Observable {

    public static final String ROOT = "/";
    public static final String WEBDIR = System.getProperty("user.home")
            + "/webroot";

    private static FileListModel instance;
    private List<FileRepresentation> files;
    private String parentDir;
    private String currentDir;
    private FileRepresentation currentDirRep;

    /**
     * Gets a Singleton instance of this class.
     * 
     * @return FileListModel Singleton instance.
     */
    public static final FileListModel getInstance() {
        if(instance == null) {
            instance = new FileListModel();
        }
        return instance;
    }

    /**
     * Default constructor
     */
    private FileListModel() {
        this.files = new ArrayList<FileRepresentation>();
    }

    /**
     * Sets a model from the given String.
     * 
     * @param fileListModelString
     *            The string to create a model from, cannot be null or empty.
     */
    public void fromString(String fileListModelString) {
        if(fileListModelString == null || fileListModelString.isEmpty()) {
            throw new IllegalArgumentException(
                    "String cannot be null or empty.");
        }

        // Remove old data
        this.files.clear();

        String[] lines = fileListModelString.split("\n");
        for(String line : lines) {
            FileRepresentation file = FileRepresentation
                    .createFileRepresentation();

            String fileName = FileRepresentationParser.getFileName(line);
            // Only add file to list when it's a true file or dir
            if(!fileName.equals(".") && !fileName.equals("..")) {
                file.setFileName(fileName);
                file.setType(FileRepresentationParser.getFileType(line));
                if(file.getType().equals(FileType.FILE)) {
                    file.setLastModified(FileRepresentationParser
                            .getLastModifiedDate(line));
                }
                file.setPath(this.getCurrentDir());
                this.files.add(file);
            }
        }
        this.setChanged();
        this.notifyObservers();
    }

    public List<FileRepresentation> getFiles() {
        return this.files;
    }

    public String getParentDir() {
        return this.parentDir;
    }

    public void setCurrentDir(String dir) {
        if(dir == null) {
            throw new IllegalArgumentException("Dir cannot be null.");
        }
        this.currentDir = dir;
    }

    public void setParentDir(String parentDir) {
        this.parentDir = parentDir;
    }

    public String getCurrentDir() {
        return this.currentDir;
    }

}
